 package server.life;

 import client.MapleCharacter;
 import client.MapleClient;
 import client.inventory.Item;
 import client.inventory.MapleInventoryType;
 import client.inventory.MaplePet;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import handling.channel.ChannelServer;
 import handling.world.WorldFindService;
 import java.awt.Point;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.maps.MapleMap;
 import tools.packet.NPCPacket;


 public final class PlayerNPC
   extends MapleNPC
 {
/*  29 */   private static final Logger log = LogManager.getLogger();
/*  30 */   private final int[] pets = new int[3];
/*  31 */   private Map<Byte, Integer> equips = new HashMap<>(); private int mapid;
   private int face;
   private int hair;

   public PlayerNPC(ResultSet rs) throws Exception {
/*  36 */     super(rs.getInt("ScriptId"), rs.getString("name"), rs.getInt("map"));
/*  37 */     this.hair = rs.getInt("hair");
/*  38 */     this.face = rs.getInt("face");
/*  39 */     this.mapid = rs.getInt("map");
/*  40 */     this.skin = rs.getByte("skin");
/*  41 */     this.charId = rs.getInt("charid");
/*  42 */     this.gender = rs.getByte("gender");
/*  43 */     setCoords(rs.getInt("x"), rs.getInt("y"), rs.getInt("dir"), rs.getInt("Foothold"));
/*  44 */     String[] pet = rs.getString("pets").split(",");
/*  45 */     for (int i = 0; i < 3; i++) {
/*  46 */       if (pet[i] != null) {
/*  47 */         this.pets[i] = Integer.parseInt(pet[i]);
       } else {
/*  49 */         this.pets[i] = 0;
       }
     }

/*  53 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  54 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs_equip WHERE NpcId = ?");
/*  55 */       ps.setInt(1, getId());
/*  56 */       ResultSet rs2 = ps.executeQuery();
/*  57 */       while (rs2.next()) {
/*  58 */         this.equips.put(Byte.valueOf(rs2.getByte("equippos")), Integer.valueOf(rs2.getInt("equipid")));
       }
/*  60 */       rs2.close();
/*  61 */       ps.close();
     }
   }
   private int charId; private byte skin; private byte gender;
   public PlayerNPC(MapleCharacter cid, int npc, MapleMap map, MapleCharacter base) throws SQLException {
/*  66 */     super(npc, cid.getName(), map.getId());
/*  67 */     this.charId = cid.getId();
/*  68 */     this.mapid = map.getId();
/*  69 */     setCoords((base.getTruePosition()).x, (base.getTruePosition()).y, 0, base.getFH());
/*  70 */     update(cid);
   }

   public static void loadAll() {
/*  74 */     List<PlayerNPC> toAdd = new ArrayList<>();
/*  75 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  76 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM playernpcs");
/*  77 */       ResultSet rs = ps.executeQuery();
/*  78 */       while (rs.next()) {
/*  79 */         toAdd.add(new PlayerNPC(rs));
       }
/*  81 */       rs.close();
/*  82 */       ps.close();
/*  83 */     } catch (Exception se) {
/*  84 */       se.printStackTrace();
     }
/*  86 */     for (PlayerNPC npc : toAdd) {
/*  87 */       npc.addToServer();
     }
   }

   public static void updateByCharId(DruidPooledConnection con, MapleCharacter chr) {
/*  92 */     if (WorldFindService.getInstance().findChannel(chr.getId()) > 0) {
/*  93 */       for (PlayerNPC npc : ChannelServer.getInstance(WorldFindService.getInstance().findChannel(chr.getId())).getAllPlayerNPC()) {
/*  94 */         npc.update(con, chr);
       }
     }
   }

   public void setCoords(int x, int y, int f, int fh) {
/* 100 */     setPosition(new Point(x, y));
/* 101 */     setCy(y);
/* 102 */     setRx0(x - 50);
/* 103 */     setRx1(x + 50);
/* 104 */     setF(f);
/* 105 */     setFh(fh);
   }

   public void addToServer() {
/* 109 */     for (ChannelServer cserv : ChannelServer.getAllInstances()) {
/* 110 */       cserv.addPlayerNPC(this);
     }
   }

   public void removeFromServer() {
/* 115 */     for (ChannelServer cserv : ChannelServer.getAllInstances()) {
/* 116 */       cserv.removePlayerNPC(this);
     }
   }

   public void update(MapleCharacter chr) {
/* 121 */     update((DruidPooledConnection)null, chr);
   }

   public void update(DruidPooledConnection con, MapleCharacter chr) {
/* 125 */     if (chr == null || this.charId != chr.getId()) {
       return;
     }
/* 128 */     setName(chr.getName());
/* 129 */     setHair(chr.getHair());
/* 130 */     setFace(chr.getFace());
/* 131 */     setSkin(chr.getSkinColor());
/* 132 */     setGender(chr.getGender());


/* 135 */     this.equips = new HashMap<>();
/* 136 */     for (Item item : chr.getInventory(MapleInventoryType.EQUIPPED).newList()) {
/* 137 */       if (item.getPosition() < -127) {
         continue;
       }
/* 140 */       this.equips.put(Byte.valueOf((byte)item.getPosition()), Integer.valueOf(item.getItemId()));
     }
/* 142 */     saveToDB(con);
   }

   public void destroy(DruidPooledConnection con) {
/* 146 */     destroy(con, false);
   }

   public void destroy(DruidPooledConnection con, boolean remove) {
/* 150 */     boolean needclose = false;
     try {
/* 152 */       if (con == null) {
/* 153 */         needclose = true;
/* 154 */         con = DatabaseConnection.getInstance().getConnection();
       }
/* 156 */       PreparedStatement ps = con.prepareStatement("DELETE FROM playernpcs WHERE scriptid = ?");
/* 157 */       ps.setInt(1, getId());
/* 158 */       ps.executeUpdate();
/* 159 */       ps.close();

/* 161 */       ps = con.prepareStatement("DELETE FROM playernpcs_equip WHERE npcid = ?");
/* 162 */       ps.setInt(1, getId());
/* 163 */       ps.executeUpdate();
/* 164 */       ps.close();
/* 165 */       if (remove) {
/* 166 */         removeFromServer();
       }
/* 168 */     } catch (Exception se) {
/* 169 */       log.error("", se);
     } finally {
/* 171 */       if (needclose) {
         try {
/* 173 */           con.close();
/* 174 */         } catch (SQLException e) {
/* 175 */           log.error("", e);
         }
       }
     }
   }

   public void saveToDB(DruidPooledConnection con) {
     try {
/* 183 */       if (getNPCFromWZ() == null) {
/* 184 */         destroy(con, true);
         return;
       }
/* 187 */       destroy(con);
/* 188 */       PreparedStatement ps = con.prepareStatement("INSERT INTO playernpcs(name, hair, face, skin, x, y, map, charid, scriptid, foothold, dir, gender, pets) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
/* 189 */       ps.setString(1, getName());
/* 190 */       ps.setInt(2, getHair());
/* 191 */       ps.setInt(3, getFace());
/* 192 */       ps.setInt(4, getSkin());
/* 193 */       ps.setInt(5, (getTruePosition()).x);
/* 194 */       ps.setInt(6, (getTruePosition()).y);
/* 195 */       ps.setInt(7, getMapId());
/* 196 */       ps.setInt(8, getCharId());
/* 197 */       ps.setInt(9, getId());
/* 198 */       ps.setInt(10, getFh());
/* 199 */       ps.setInt(11, getF());
/* 200 */       ps.setInt(12, getGender());
/* 201 */       String[] pet = { "0", "0", "0" };
/* 202 */       for (int i = 0; i < 3; i++) {
/* 203 */         if (this.pets[i] > 0) {
/* 204 */           pet[i] = String.valueOf(this.pets[i]);
         }
       }
/* 207 */       ps.setString(13, pet[0] + "," + pet[1] + "," + pet[2]);
/* 208 */       ps.executeUpdate();
/* 209 */       ps.close();

/* 211 */       ps = con.prepareStatement("INSERT INTO playernpcs_equip(npcid, charid, equipid, equippos) VALUES (?, ?, ?, ?)");
/* 212 */       ps.setInt(1, getId());
/* 213 */       ps.setInt(2, getCharId());
/* 214 */       for (Map.Entry<Byte, Integer> equip : this.equips.entrySet()) {
/* 215 */         ps.setInt(3, ((Integer)equip.getValue()).intValue());
/* 216 */         ps.setInt(4, ((Byte)equip.getKey()).byteValue());
/* 217 */         ps.executeUpdate();
       }
/* 219 */       ps.close();
/* 220 */     } catch (Exception se) {
/* 221 */       log.error("", se);
     }
   }

   public MapleCharacter getPlayer() {
/* 226 */     return MapleCharacter.getCharacterById(this.charId);
   }

   public Map<Byte, Integer> getEquips() {
/* 230 */     return this.equips;
   }

   public byte getSkin() {
/* 234 */     return this.skin;
   }

   public void setSkin(byte s) {
/* 238 */     this.skin = s;
   }

   public int getGender() {
/* 242 */     return this.gender;
   }

   public void setGender(int g) {
/* 246 */     this.gender = (byte)g;
   }

   public int getFace() {
/* 250 */     return this.face;
   }

   public void setFace(int f) {
/* 254 */     this.face = f;
   }

   public int getHair() {
/* 258 */     return this.hair;
   }

   public void setHair(int h) {
/* 262 */     this.hair = h;
   }

   public int getCharId() {
/* 266 */     return this.charId;
   }

   public int getMapId() {
/* 270 */     return this.mapid;
   }

   public int getPet(int i) {
/* 274 */     return (this.pets[i] > 0) ? this.pets[i] : 0;
   }

   public void setPets(List<MaplePet> p) {
/* 278 */     for (int i = 0; i < 3; i++) {
/* 279 */       if (p != null && p.size() > i && p.get(i) != null) {
/* 280 */         this.pets[i] = ((MaplePet)p.get(i)).getPetItemId();
       } else {
/* 282 */         this.pets[i] = 0;
       }
     }
   }


   public void sendSpawnData(MapleClient client) {
/* 289 */     client.announce(NPCPacket.spawnNPC(this));
/* 290 */     client.announce(NPCPacket.spawnPlayerNPC(this));
/* 291 */     client.announce(NPCPacket.spawnNPCRequestController(this, true));
   }

   public MapleNPC getNPCFromWZ() {
/* 295 */     MapleNPC npc = MapleLifeFactory.getNPC(getId(), getMapId());
/* 296 */     if (npc != null) {
/* 297 */       npc.setName(getName());
     }
/* 299 */     return npc;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\life\PlayerNPC.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */